package com.tt.fashionWebSite.service.imp;

import com.tt.fashionWebSite.dao.PhanQuyenDao;
import com.tt.fashionWebSite.dao.SoDoCoTheDao;
import com.tt.fashionWebSite.dao.ThongTinThanhVienDao;
import com.tt.fashionWebSite.domain.SoDoCoThe;
import com.tt.fashionWebSite.domain.ThongTinThanhVien;
import com.tt.fashionWebSite.dto.JsonResponseCode;
import com.tt.fashionWebSite.dto.ThongTinThanhVienDto;
import com.tt.fashionWebSite.service.Ad_MemberService;
import com.tt.fashionWebSite.service.GenericService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service("ad_memberService")
public class Ad_MemberServiceImp extends GenericService implements Ad_MemberService {
    @Autowired
    @Qualifier("thongTinThanhVien")
    private ThongTinThanhVienDao thongTinThanhVienDao;

    @Autowired
    @Qualifier("soDoCoThe")
    private SoDoCoTheDao soDoCoTheDao;

    @Autowired
    @Qualifier("phanQuyen")
    private PhanQuyenDao phanQuyenDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int registerMember(ThongTinThanhVienDto memberDto) {
        ThongTinThanhVien thongTinThanhVien = thongTinThanhVienDao.findById(memberDto.getMaTV());
        if(thongTinThanhVien == null){
            thongTinThanhVien = new ThongTinThanhVien();
            thongTinThanhVien.setMaTV(memberDto.getMaTV());
            thongTinThanhVien.setHo(memberDto.getHo());
            thongTinThanhVien.setTen(memberDto.getTen());
            thongTinThanhVien.setDiaChi(memberDto.getDiaChi());
            thongTinThanhVien.setGioiTinh(memberDto.getGioiTinh());
            thongTinThanhVien.setNgaySinh(memberDto.getNgaySinh());
            thongTinThanhVien.setSoDT(memberDto.getSoDT());
            thongTinThanhVienDao.insert(thongTinThanhVien);
            return JsonResponseCode.SUCCESS;
        }
        else {
            return JsonResponseCode.FAILED;
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public ThongTinThanhVienDto registerMemberEdit(ThongTinThanhVienDto memberDto) {
        ThongTinThanhVien thongTinThanhVien = thongTinThanhVienDao.findById(memberDto.getMaTV());
        try {
            thongTinThanhVien.setMaTV(memberDto.getMaTV());
            thongTinThanhVien.setHo(memberDto.getHo());
            thongTinThanhVien.setTen(memberDto.getTen());
            thongTinThanhVien.setDiaChi(memberDto.getDiaChi());
            thongTinThanhVien.setGioiTinh(memberDto.getGioiTinh());
            thongTinThanhVien.setNgaySinh(memberDto.getNgaySinh());
            thongTinThanhVien.setSoDT(memberDto.getSoDT());
            thongTinThanhVienDao.update(thongTinThanhVien);
            return memberDto;
        }
        catch (Exception ex) {
            return null;
        }
    }

    @Override
    public List<ThongTinThanhVienDto> getMemberInfor() {
        return mapper.mapCollection(thongTinThanhVienDao.getMemberInfor(), ThongTinThanhVienDto.class);
    }

    public ThongTinThanhVienDto getMemberInfoEdit(ThongTinThanhVienDto memberDto){
        ThongTinThanhVien thongTinThanhVien = thongTinThanhVienDao.findById(memberDto.getMaTV());
        ThongTinThanhVienDto thongTinThanhVienDto = mapper.map(thongTinThanhVien, ThongTinThanhVienDto.class,THONG_TIN_THANH_VIEN_MAP_ID);
        return  thongTinThanhVienDto;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public List<ThongTinThanhVienDto> memberInfoDelete(List<ThongTinThanhVienDto> listUser){
        for(int i = 0; i < listUser.size(); i++) {
            phanQuyenDao.delete(listUser.get(i).getMaTV());
            thongTinThanhVienDao.delete(listUser.get(i).getMaTV());
        }
        return mapper.mapCollection(thongTinThanhVienDao.getMemberInfor(), ThongTinThanhVienDto.class);
    }
}
